<template>
  <div>
    <el-form-item label="跟踪方式"
                  prop="pactCode">
      <!-- <number-type :fromCityName="form.fromAddress.city"
                       :toCityName="form.toAddress.city" /> -->
      <select-device :form-info="form"
                     :process="1"
                     :num="0"
                     :flag="true"
                     v-on="$listeners"
                     @input="handleInput" />
      <current-place-show v-if="
          form.deviceCode &&
          form.trackType == 1 &&
          !['batchWaybill', 'onebill'].includes(transportType)
        "
                          :from-city-name="form.fromAddress.city"
                          :to-city-name="form.toAddress.city"
                          :first-device="form.deviceCode"
                          :form-info="form" />
    </el-form-item>
    <el-form-item v-if="
        form.deviceCode &&
        form.trackType == 1 &&
        !['batchWaybill', 'onebill'].includes(transportType)
      "
                  label="多式联运"
                  prop="transport">
      <el-checkbox-group v-model="checked"
                         :max="1"
                         size="medium"
                         @change="handleCheckedChange">
        <el-checkbox-button v-for="item in checkOptions"
                            :key="item"
                            :label="item">{{ item }}</el-checkbox-button>
      </el-checkbox-group>
      <electronic-equipment :form-info="form"
                            @refreshPaterForm="refreshPaterForm" />
    </el-form-item>
  </div>
</template>

<script>
import SelectDevice from "./SelectDevice.vue";
import CurrentPlaceShow from "./CurrentPlaceShow.vue";
import ElectronicEquipment from "./ElectronicEquipment.vue";

export default {
  name: "NumberType",
  components: {
    SelectDevice,
    CurrentPlaceShow,
    ElectronicEquipment
  },
  props: {
    formInfo: {
      type: Object,
      default: () => { }
    },
    transportType: {
      type: String,
      default: () => null
    },
    newFormFlag: {
      type: Boolean,
      default: () => false
    }
  },
  data () {
    return {
      form: {},
      checkOptions: ["火车", "飞机", "电子回单"],
      checked: []
    };
  },
  created () {
    this.form = this.newFormFlag ? { ...this.formInfo } : this.formInfo;

    setTimeout(() => {
      if (this.form.checkValue) {
        switch (this.form.checkValue) {
          case 1:
            return (this.checked = ["飞机"]);
          case 2:
            return (this.checked = ["火车"]);
          case 3:
            return (this.checked = ["电子回单"]);
        }
      }
    }, 500);
  },
  methods: {
    handleInput (code, process, params) {
      if (process === 1) {
        this.form.trackType = params ? params : this.form.trackType;
        if (this.form.trackType === 1) {
          this.form.deviceCode = code;
          this.form.thirdDeviceCode = this.form.thirdDeviceCode
            ? this.form.thirdDeviceCode
            : code;
        } else {
          this.form.deviceCode = code;
        }
      }
    },
    // 修改跟踪方式
    handleRadio (val) {
      this.form.checkValue = val;
      if (val != 1) {
        this.form.flyTime = null;
        this.form.flyNo = null;
      } else if (val != 2) {
        this.form.trainNum = null;
        this.form.trainTime = null;
        this.form.trainForm = null;
        this.form.trainTo = null;
      }

      if (val != 3) {
        for (let key in this.form.secordReceiptAddress) {
          this.form.secordReceiptAddress[key] = "";
        }
      }
      this.form.secondDeviceCode = null;
      this.form.thirdDeviceCode = null;
      this.form.isThirdShow = val === 3 ? false : true;
      this.form.currentAddress = [];
      for (let key in this.form.receiptAddress) {
        this.form.receiptAddress[key] = "";
      }
    },
    handleCheckedChange (value) {
      if (value && value.length > 0) {
        let val = value[0];
        switch (val) {
          case "火车":
            return this.handleRadio(2);
          case "飞机":
            return this.handleRadio(1);
          case "电子回单":
            return this.handleRadio(3);
        }
      } else {
        this.form.checkValue = null;
        this.form.currentAddress = [];
        for (let key in this.form.receiptAddress) {
          this.form.receiptAddress[key] = "";
        }
        this.form.secordReceiptAddress = { ...this.form.receiptAddress };
        this.form.isThirdShow = true;
        this.secondDeviceCode = "";
        this.thirdDeviceCode = "";
        this.form.flyNo = null;
        this.form.flyTime = null;
        this.form.trainNum = null;
        this.form.trainTime = null;
        this.form.trainForm = null;
        this.form.trainTo = null;
      }
    },
    // 将form回传父组件
    refreshPaterForm () {
      console.log(this.form)
      if (this.newFormFlag) this.$emit("update:formInfo", this.form);
    }
  }
};
</script>
